package com.example.addressbook.controller;


import com.example.addressbook.dao.GroupRepository;
import com.example.addressbook.dao.TelRepository;
import com.example.addressbook.entities.Grouping;
import com.example.addressbook.entities.Linkman;
import com.example.addressbook.entities.Tel;
import com.example.addressbook.service.LinkmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

//联系人控制层
@Controller
@RequestMapping("linkman")
public class LinkmanController {

    @Autowired
    LinkmanService linkmanService;

    @Autowired
    GroupRepository groupRepository;

    @Autowired
    TelRepository telRepository;

    //新建联系人
   @RequestMapping("/new")
    public String new_contacts(Model model){
       Linkman lm=new Linkman();
       List<Grouping> groupings=groupRepository.findAll();
       List<Tel> tels=telRepository.findAll();
        model.addAttribute("groupings",groupings);
        model.addAttribute("Linkman",lm);
        model.addAttribute("tels",tels);
        return "new-contacts";
    }

    //更新联系人
    @RequestMapping("/new2")
    public String new2_contacts(Model model){
        Linkman lm2=new Linkman();
        model.addAttribute("Linkman",lm2);
        return "new-contacts2";
    }

    //保存联系人
    @PostMapping("/save")
    public String save_contacts(Model model , @RequestParam List<Long> tels, @Valid Linkman linkman, Errors errors){

        if(errors.hasErrors()){
            return "new-contacts";
        }

        Iterable<Tel> allById = telRepository.findAllById(tels);

        for (Tel tel:allById) {
            tel.setLinkman(linkman);
            linkmanService.save(linkman);
        }


        return "redirect:/linkman/new";
    }

//保存更新之后的联系人
    @PostMapping("/save2")
    public String save2_contacts(Model model , Linkman linkman){
        linkmanService.save(linkman);
        return "redirect:/linkman/new2";
    }

//联系人list列表
    @GetMapping("/")
    public String displayLinkman(Model model){
        List<Linkman> displayLinkman = linkmanService.getAll();
        model.addAttribute("displayLinkman",displayLinkman);
        return "display_linkman";
    }

//更新操作
    @GetMapping("/update")
    public String displayLinkmanUpdateForm(@RequestParam("id")long thID, Model model){

       Linkman linkman=linkmanService.findById(thID);
       model.addAttribute("Linkman",linkman);
       return "new-contacts2";       //更新联系人前端
   }

//删除操作 出现错误 未完成
   @GetMapping("/delete")
    public String displayLinkmanDeleteForm(@RequestParam("id")long thID, Model model){
       Linkman linkman=linkmanService.findById(thID);
       linkmanService.delete(linkman);
       return "redirect:/linkman/";

    }





}
